/**
 * 
 */
package com.wisdytech.linkdcs.video.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pagehelper.PageInfo;
import com.wisdytech.linkdcs.video.dao.IEqVideoDao;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.constants.CommonConstants;
import com.wisdytech.common.exception.ServiceException;
import com.wisdytech.common.handler.BaseService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.linkdcs.equipment.model.Client;
import com.wisdytech.linkdcs.video.model.EquipmentVideo;
import com.wisdytech.linkdcs.video.service.IEquipmentVideoService;

/**
 * <p>
 * ClassName: EquipmentVideoServiceImpl
 * </p>
 * <p>
 * Description: 视频设备管理业务层的实现类
 * </p>
 * <p>
 * Author: kongming
 * </p>
 * <p>
 * Date: 2017-10-31
 * </p>
 */
@Service("equipmentVideoService")
@Transactional
public class EquipmentVideoServiceImpl extends BaseService<IEqVideoDao, EquipmentVideo> implements IEquipmentVideoService {
    
    @Resource
    private IEqVideoDao videoDao;
    
    @Override
    public PageInfo<EquipmentVideo> listEquipmentVideoByFilter(String code, Filter filter) {
        EntityWrapper<EquipmentVideo> ew = new EntityWrapper<>();
        if (StringUtils.isNotBlank(code)) {
            ew.like("code", code);
        }
        ew.orderBy("create_date", false);
        List<EquipmentVideo> lists = super.beforeList(IEqVideoDao.class, filter).selectList(ew);
        PageInfo<EquipmentVideo> videoPage=new PageInfo<>(lists);
        return videoPage;
    }

    @Override
    public StandardResult addEquipmentVideo(EquipmentVideo eqVideo) {
        EntityWrapper<EquipmentVideo> ew =new EntityWrapper<>();
        //验证视频设备编号的唯一性
        List<EquipmentVideo> videoList;
        videoList = videoDao.selectPage(new Page<Client>(1, 1), ew.eq("code", eqVideo.getCode()));
        if (!videoList.isEmpty()) {
            return StandardResult.getFailResult(CommonConstants.EQUIPMENT_VIDEO);
        }
        eqVideo.setPkId(BaseUtil.getUUID());
        eqVideo.setCreateDate(new Date());
        int count = videoDao.insert(eqVideo);
        if (count <=0){
            throw new ServiceException("新增失败");
        }
        return StandardResult.getSuccessResult();
    }
    @Override
    public StandardResult updateEquipmentVideo(EquipmentVideo eqVideo) {
        EntityWrapper<EquipmentVideo> ew =new EntityWrapper<>();
      //验证视频设备编号的唯一性
        List<EquipmentVideo> videoList;
        videoList = videoDao.selectPage(new Page<Client>(1, 1), ew.eq("code", eqVideo.getCode()).ne("pk_id", eqVideo.getPkId()));
        if (videoList.size() > 0){
            return StandardResult.getFailResult(CommonConstants.EQUIPMENT_VIDEO);
        }
        eqVideo.setUpdateDate(new Date());
        int count = videoDao.updateById(eqVideo);
        if (count<=0) {
            throw new ServiceException("修改失败");
        }
        return StandardResult.getSuccessResult();
    }
    @Override
    public EquipmentVideo getEquipmentVideoById(String pkId) {
        return videoDao.selectById(pkId);
    }
    @Override
    public StandardResult delEquipmentVideoById(String id) {
        List<String> idList = new ArrayList<String>();
        String[] ids = id.split(",");
        for (int i = 0; i < ids.length; i++) {
            idList.add(ids[i]);
        }
        int count = videoDao.deleteBatchIds(idList);
        if (count <= 0) {
            throw new ServiceException("删除失败");
        }
        return StandardResult.getSuccessResult();
    }

}
